<?php
if (!defined('CAL_GREGORIAN')) {
	define('CAL_GREGORIAN', 1);
}
if (!function_exists('cal_days_in_month')) {
	function cal_days_in_month($calendar, $month, $year) {
		return date('t', mktime(0, 0, 0, $month, 1, $year));
	}
}
class DatePrice extends CWidget {
	
	public $model = null;
	public $id;
	public $date;
	protected $_year;
	protected $_month;
	protected $_day;
	
	public function run() {
		if(!$this->id) return;
		if(!$this->date) $this->date = date('Y-m-d');
		$this->model = $this->model = Travel::model()->findByPk($this->id);
		list($this->_year, $this->_month, $this->_day) = explode('-', $this->date);
		
		$this->render('datePrice', array(
			'year' => $this->_year,
			'month' => $this->_month,
		));
	}
	
	public function getNextMonth() {
		return $this->_month < 12 ? $this->_month+1 : 1;
	}
	
	public function getPrevMonth() {
		return $this->_month > 1 ? $this->_month-1 : 12;
	}
	
	public function getPrevUrl() {
		$date = date('Y-m-d', strtotime('-1 month', strtotime($this->date)));
		return $this->getController()->createUrl('/datePrice/getView', array(
			'id' => $this->id,
			'date' => $date,
		));
	}
	
	public function getNextUrl() {
		$date = date('Y-m-d', strtotime('+1 month', strtotime($this->date)));
		return $this->getController()->createUrl('/datePrice/getView', array(
			'id' => $this->id,
			'date' => $date,
		));
	}
	
	public function renderContent() {
		$year = $this->_year;
		$month = $this->_month;
		$day = 1;
		
		$maxDay = cal_days_in_month(CAL_GREGORIAN, $month, $year);
		$pad = date('N', strtotime("{$year}-{$month}-{$day}"));
		$pad = $pad == 7 ? 0 : $pad;

		$model = $this->model;
		$rule = $model->buildRule;
		$today = date('Y-n-j');
		for($i=0; $i<6; ++$i) {
			echo '<tr>';
			for($j=0; $j<7; ++$j) {
				if($pad > 0 || $day > $maxDay) {
					echo $this->formatCell();
					--$pad;
					continue;
				} else {
					$isValid = $this->dateIsValid($year, $month, $day);
					echo $this->formatCell(
						$year, $month, $day, 
						$model->priceInfo()->price_agent, 
						!$isValid
					);
				}
				++$day;
			}
			echo '</tr>';
		}
	}
	
	public function dateIsValid($y, $m, $d) {
		$rule = $this->model->buildRule;
		$intDate = strtotime("{$y}-{$m}-{$d}");
		$strDate = date('Y-m-d', $intDate);
		$rIntDate = strtotime($rule->start_time); 
		list($iY, $iM, $iD) = explode('-', date('Y-m-d', $intDate));
		if($strDate < $rule->start_time || ($rule->end_type == 1 && $strDate > $rule->end_time)) {
			return false;
		}
		// 不能小于今天
		if ($strDate < date('Y-m-d')) {
			return false;
		}
		
		switch($rule->build_type) {
			case 'ByWeek': {
				$week = date('N', $intDate);
				return in_array($week, $rule->by_week);
			}
			case 'ByDay': {
				$diffDay = ($intDate - $rIntDate) / 3600 / 24;
				return $diffDay%$rule->by_day == 0;
			}
			case 'ByDate': {
				return in_array($strDate, $rule->by_date);
			}
			default: return true;
		}
		
	}
	
	public function getDayStr($year, $month, $day) {
		$ymdStr = $year.$month.$day;
		$mdStr = intval($month).intval($day);
		static $days = array(
			'101' => '国庆节',
			'81' => '建军节',
			'11' => '元旦节',
			'214' => '情人节',
			'51' => '劳动节',
			'61' => '儿童节',
			'312' => '植树节',
			'41' => '愚人节',
			'44' => '清明节',
			'54' => '青年节',
			'910' => '教师节',
			'1111' => '光棍节',
			'1225' => '圣诞节',
		);
		
		if(date('Ymd') == $ymdStr) {
			return '今天';
		} elseif(isset($days[$mdStr])) {
			return $days[$mdStr];
		} else {
			return $day;
		}
	}
	
	public function formatCell($y = null, $m = null, $d = null, $price = null, $invalid = false) {
		
		$dayStr = $y&&$m&&$d ? $this->getDayStr($y, $m, $d) : '&nbsp;';
		$value = date('Y-m-d', strtotime("{$y}-{$m}-{$d}"));
		$class = $invalid || $dayStr == '&nbsp;' ? ' invalid' : ' valid';
		$price = $price ? '￥'.$price : '&nbsp;';
		$str =  "<td val='{$value}' class='{$class}'><div class='dp-cell-day{$class}'>{$dayStr}</div>".
				"<div class='dp-cell-price{$class}'>{$price}</div></td>";
		
		return $str;
	}
	

}

?>